
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class GraphTest {

	public static void main(String[] args) {
		final Frame f = new Frame("Draw");

		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				f.dispose();
			}
		});

		f.setSize(400, 300);
		final DrawCanvas c = new DrawCanvas();

		f.add(c);
		f.setVisible(true);

	}
}

class NewMyFrame extends Frame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JFrame frame;
	// JComponent c;
	JPanel p, p1, p2, p3;
	FlowLayout fl;
	GridBagLayout gbl;
	LineBorder lb;
	Color c1, c2, c3, c4;

	NewMyFrame() {

		// frame = new JFrame();
		gbl = new GridBagLayout();
		fl = new FlowLayout(1, 1, 1);
		c1 = new Color(255, 255, 240);
		c2 = new Color(240, 255, 240);
		c3 = new Color(255, 240, 240);
		c4 = new Color(50, 50, 200);
		lb = new LineBorder(c4, 1, true);
		p = new JPanel(gbl);
		p1 = new JPanel(fl);
		p2 = new JPanel(fl);
		p3 = new JPanel(fl);

		p1.setBorder(lb);
		p1.setPreferredSize(new Dimension(200, 200));
		p1.setBackground(c1);
		p2.setBorder(lb);
		p2.setPreferredSize(new Dimension(100, 301));
		p2.setBackground(c2);
		p3.setBorder(lb);
		p3.setPreferredSize(new Dimension(200, 100));
		p3.setBackground(c3);
		p.add(p1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, 18, 0,
				new Insets(1, 1, 0, 0), 0, 0));
		p.add(p2, new GridBagConstraints(1, 0, 1, 2, 1.0, 1.0, 12, 0,
				new Insets(1, 1, 1, 1), 0, 0));
		p.add(p3, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, 10, 0,
				new Insets(1, 1, 1, 0), 0, 0));
		frame.setTitle("Test Window");
		frame.setCursor(new Cursor(1));
		frame.setLocation(150, 150);
		frame.setDefaultCloseOperation(3);
		frame.setResizable(false);
		frame.getContentPane().add(p);
		frame.pack();
		frame.setVisible(true);

	}
}

class DrawCanvas extends Canvas {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int lastX, lastY;
	//private int ex, ey;
	//private boolean clear = false;

	/*
	 * public DrawCanvas () { super(); addMouseListener(new MouseAdapter() {
	 * public void mousePressed(MouseEvent e) { lastX = e.getX(); lastY =
	 * e.getY();
	 * 
	 * } });
	 * 
	 * addMouseMotionListener(new MouseMotionAdapter() { public void
	 * mouseDragged(MouseEvent e) { ex=e.getX(); ey=e.getY(); repaint();
	 * 
	 * } });
	 * 
	 * addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { if
	 * (e.getKeyChar()==' ') { clear = true; repaint(); } } });
	 * 
	 * }
	 * 
	 * public void update(Graphics g) { if (clear) { g.clearRect(0, 0,
	 * getWidth(), getHeight()); clear = false; } else { g.drawLine(lastX,
	 * lastY, ex, ey); lastX=ex; lastY=ey; } }
	 */

	public void paint(Graphics g) {
		boolean chek = true;
		// g.drawOval(0, 0, lastX+10, lastY+10);

		for (int i = 0; i < getWidth();) {

			g.setColor(getForeground());
			g.drawOval(lastX, 0, 10, 10);
			// g.setColor(getBackground());
			// g.drawOval(lastX , lastY, lastX+10, lastY+10);
			// g.clearRect(0, 0, getWidth(), getHeight());
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			g.setColor(getBackground());
			g.drawOval(lastX, 0, 10, 10);

			lastX = chek ? i++ : i--;
			if (i == getWidth() - 1)
				chek = false;
			if (i == 1)
				chek = true;
		}
	}
}